import Vue from 'vue'
import VueRouter from 'vue-router'

const register = () => import('../views/Register');
const login = () => import('../views/Login');
const main = () => import('../views/pc/Main');
const mobile = () => import('../views/Mobile');
const blog = () => import('../views/blog/Blog');
const article = () => import('../views/blog/Article');
const post = () => import('../views/blog/Post');
const test = () => import('../views/test');

Vue.use(VueRouter)

const routes = [
    {
        path: '/register',
        name: 'register',
        component: register,
        meta: {isPublic: true}
    },
    {
        path: '/login',
        name: 'login',
        component: login,
        meta: {isPublic: true}
    },
    {
        path: '/',
        name: 'Main',
        component: main
    },
    {
        path: '/mobile',
        name: 'Mobile',
        component: mobile,
    },
    {
        path: '/blog/:uid',
        name: 'Blog',
        component: blog,
        props: true,
        meta: {isPublic: true},
    },
    {
        path: '/blog/:uid/:aid',
        name: 'Article',
        component: article,
        props: true,
        meta: {isPublic: true}
    },
    {
        path: '/postBlog/:uid',
        name: 'Post',
        component: post,
        props: true,
    },
    {
        path: '/test',
        name: 'Test',
        component: test,
        meta: {isPublic: true}
    },
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    if (!to.meta.isPublic && !sessionStorage.token) {
        alert('请先登录');
        return next('/login');
    }
    next();
})

export default router